<?php
namespace Management\Controller;
use Think\Controller;
use Myself\FileSystem;
class ReplyController extends BaseController {

	protected $m = NULL;
	protected $m_class = NULL;
	protected $m_config = NULL;
    protected $statusArr=array("停用","启用");
	
	public function _initialize(){
		parent::_initialize();
		$this->m = M('appraise_sku');
		// $this->m_class = M('news_class');
		// $this->m_config = M('news_config');	
	}
		
	
	
    public function index(){ 
		header('Content-Type:text/html;charset=utf-8');
		$where=[];
		//-----------------搜索-------------------
        $key = I('key','');//关键字 订单号
		if(!empty($key)){
			$where['text'] = array('LIKE','%'.$key.'%');
		}
		$sta = I('sta','');//关键字 订单号
		if(strlen($sta) != 0){
			$where['status'] = $sta;
			$this->assign('sta',$sta);
		}
		
		$from_time = I('from_time','');
		$to_time = I('to_time','');
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
		}
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
		}
		if($from_time && $to_time){
			$where['addtime']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}
		//-------------------------搜索end-----------------
		$pageno = I('p',1,'int');
		$pageCount = 10;
		$list = $this->m->where($where)->page($pageno.','.$pageCount)->select();
		foreach($list as $k=>$v){
			$member = M('member')->find($v['member_id']);
			$list[$k]['member'] = $member['username'];
			$sku = M('sku')->find($v['sku_id']);
			$list[$k]['sku'] = json_decode($sku['attribute_cn']);
			// $list[$k]['pics'] = json_decode($list[$k]['pics']);
		}
		
		$this->assign('list',$list);// 赋值数据集
		$count = $this->m->where($where)->count();// 查询满足要求的总记录数
		$this->assign('count',$count);// 赋值分页输出	
		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->showManage();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
        //-------------------------固定参数输出-----------------
        $this->assign('statusArr',$this->statusArr);// 状态
		$this->display('index');
    }
	public function edit(){
		
		
		$id = I('id',0,'int');
		$reply = $this->m->find($id);
		$member = M('member')->find($reply['member_id']);
		$reply['member'] = $member['username'];
		$reply['pics'] = json_decode($reply['pics']);
		// $spu = M('spu')->find($reply['spu_id']);
		// $reply['spu'] = $spu;
		// $sku = M('sku')->find($reply['sku_id']);
		// $reply['sku'] = json_decode($sku['attribute_cn']);
		$order_detail = M('order_detail')->find($reply['order_detail_id']);
		$order_detail['attribute_cn'] = json_decode($order_detail['attribute_cn']);
		$reply['order_detail'] = $order_detail;
		// echo "<pre>";
		// var_dump($reply);
		// die;
		$this->assign('reply',$reply);

		$this->assign('back_url',$_SERVER['HTTP_REFERER']);

        $this->assign('statusArr',$this->statusArr);// 状态
        $this->assign('lang',I('get.lang','cn'));

		$this->display('edit');
    }
	public function save(){
		
		$temp = $this->m->create();
		if($temp['id']){
			
			$this->m->save($temp);
			$this->selectPage_ajax(array('act' => '修改', 'back_url' => I('back_url'),'jump_url' => U('Reply/edit','id='.$temp['id'])));
		}else{
		    $result = $this->m->add($temp);
			if($result)
			    $this->selectPage_ajax( array('act' => '添加', 'back_url' => I('back_url'),'jump_url' => U('Reply/edit')));

			else $this->error($GLOBALS['notice']['error'][0]);
		}
	
	}
	public function del(){

		
		$ids = I('id');

		if($ids){
			if(is_array($ids)){$ids=implode(",",$ids);}
			$this->baseDelFile('ticket',$ids,'pic_path');
			if ($this->m->delete($ids)){
				$ids=explode(",",$ids);
			    $this->success($GLOBALS['notice']['success'][1]);
			}else{
				$this->error($GLOBALS['notice']['error'][1]);
			}
		}else{
			$this->error($GLOBALS['notice']['warming'][1]);
		}		
    }
	public function batch(){          ////批量操作方法

		
		$ids = I('id');
		$val = I('get.val');
		$field = I('get.field');
		
		
		
		if($ids){
			if(is_array($ids)){$ids=implode(",",$ids);}
			
			if ($this->m->where('id in ('.$ids.')')->save(array($field=>$val))){
				$ids=explode(",",$ids);
                if($field=='status'){
                    $spuList=$this->m->where(['id'=>['in',$ids]])->field('spu_id')->select();
                    if($val==1){
                        foreach ($spuList as $item){
                            M('spu')->where(['id'=>$item['spu_id']])->setInc('appraise');
                        }
                    }else
                        foreach ($spuList as $item){
                            M('spu')->where(['id'=>$item['spu_id']])->setDec('appraise');
                        }

                }
			    $this->success($GLOBALS['notice']['success'][2]);
			}else{
				$this->error($GLOBALS['notice']['error'][2]);
			}
		}else{
			$this->error($GLOBALS['notice']['warming'][1]);
		}		
    }
	public function ajaxFindNext(){

		$selValue = I('selValue','0','int');
		$areaNextId = I('areaNextId','','int');
		$domId = I('domId');
		$classList = $selValue?$this->m_class->where('parent_id='.$selValue)->select():array();
		$content = "";
		if(!count($classList)){
			echo 0;exit;
		}else{
		
			$content .= '<span id="'.$domId.'_'.$areaNextId.'">'."\n\t";
			$content .= '<select class="form-control input-small" name="class_id[]" onchange="javascript:findNext(this.value , '.$areaNextId.' , \''.$domId.'\')">'."\n\t";
			$content .= '<option value="0">请选择</option>'."\n\t";
			foreach($classList as $item){
			$selectedMark = $selValue==$item['id']?'selected':'';
			$content .= '<option value="'.$item['id'].'" '.$selectedMark.'>'.$item['class_name'].'</option>'."\n\t";
			}
			$content .= '</select>'."\n\t";
			$content .= '</span>'."\n\t";
		
			echo $content;exit;
		
		}
	}
	public function ajaxListInput(){
		header('Content-Type:text/html;charset=utf-8');
		$info = I('post.');
		$data[$info["fieldName"]] = $info["Val"];
		$data["id"] = $info["id"];
		if ($this->m->save($data)){
            if($info["fieldName"]=='status'){
                $spu_id=$this->m->where(['id'=>$data["id"]])->getField('spu_id');
                if($info["Val"]==1){
                    M('spu')->where(['id'=>$spu_id])->setInc('appraise');
                }else
                    M('spu')->where(['id'=>$spu_id])->setDec('appraise');

            }
			echo "success";
		}else{
		    echo $this->m->getlastsql();
			echo "error";
		}
		exit;
	}
	
	public function goodajax(){
		$id = I('id');
		$goodsecond = M('spu_category')->where('parent_id ='.$id)->select();
		$this->ajaxReturn($goodsecond);
	}
	
	
}